package com.example.mqtt.config;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

/**
 * 发布
 **/
@Configuration
@Slf4j
public class MqttOutboundConfig {
    @Resource
    private MqttProperty mqttProperty;
    @Resource
    private MqttPahoClientFactory mqttClientFactory;

    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }


    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MessageHandler mqttOutboundMessageHandler() {
        MqttPahoMessageHandler m = new MqttPahoMessageHandler(mqttProperty.getUrl(), mqttProperty.getPubClientId(), mqttClientFactory);
        m.setDefaultQos(mqttProperty.getQos());
        m.setAsync(true);
        return m;
    }

}
